package web;

import driverSelf.ChromeDr;
import driverSelf.EdgeDr;
import driverSelf.FireFoxDr;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.concurrent.ExecutionException;

public class GetMsg {
    private WebDriver driver;

    public WebDriver getDriver() {
        return driver;
    }

    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    public void openBrowser(String browserType) {
        switch (browserType) {
            case "火狐":
                FireFoxDr firefox = new FireFoxDr("", "webdriverExe\\geckodriver.exe");
                driver = firefox.getDriver();
                break;
            case "Edge":
                EdgeDr edge = new EdgeDr("", "webdriverExe\\msedgedriver.exe");
            case "chrome":
            default:
                ChromeDr chrome = new ChromeDr("chromedriver.exe");
                driver = chrome.getDriver();

        }
    }

    public void visitPage(String url) {
        try {
            driver.get(url);
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
            System.out.println("url请检查是否正确");
        }
    }

    public void setWindowSize(int leftCorner, int height, int width) {
        driver.manage().window().setPosition(new Point(leftCorner, 0));
        driver.manage().window().setSize(new Dimension(height, width));
    }

    public void clickButton(String xpath1) {
        try {
            driver.findElement(By.xpath(xpath1)).click();
        } catch (Exception e) {
            System.out.println(e.fillInStackTrace());
        }
    }

    public void jumpPage() {
        String winHandleBefore = driver.getWindowHandle();
        for (String winHandle : driver.getWindowHandles()) {
            if (winHandle.equals(winHandleBefore)) {
                continue;
            }
            driver.switchTo().window(winHandle);
            break;
        }
    }

    public void switchFrame(String xpath1) {
        try {
            if (xpath1.startsWith("#")) {
                WebElement frame = driver.findElement(By.id(xpath1));
                driver.switchTo().frame(frame);

            } else {
                driver.switchTo().frame(xpath1);
            }
        } catch (Exception e) {

        }
    }
    public void exWaitElement(String xpath){
        WebDriverWait wait=new WebDriverWait(driver,10000);
        //编写自己的等待条件，等待某个元素能定位到。
        ExpectedCondition<WebElement> waitElement = new ExpectedCondition<WebElement>() {
            public WebElement apply(WebDriver driver) {
                WebElement element = driver.findElement(By.xpath(xpath));
                return element;
            }

            public String toString() {
                return String.format("通过xpath"+xpath+"没有定位到指定元素，所以10秒之后报错");
            }
        };

        wait.until(waitElement);

    }
    public void sendKeys(String xpath1,String content) {
        driver.findElement(By.xpath(xpath1)).sendKeys(content);
    }
    public void selectItems(String xpath1,int index){
        WebElement elem=driver.findElement(By.xpath(xpath1));
        Select select=new Select(elem);
        select.selectByIndex(index);
    }
    public void selectItems(String xpath1,String value){
        WebElement elem=driver.findElement(By.xpath(xpath1));
        Select select=new Select(elem);
        select.selectByValue(value);
    }
    public void assertEleAttr(String xpath, String attr, String expectedResult) {
        try {
            WebElement element = driver.findElement(By.xpath(xpath));
            String attribute = element.getAttribute(attr);
            if (attribute.equals(expectedResult)) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
                System.out.println("预期结果是" + expectedResult + "，实际结果是" + attribute);
            }
        } catch (Exception e) {
            System.out.println("测试失败，获取元素的属性出错了");
        }
    }
    public void clearMsg(String xpath1){
        WebElement elem=driver.findElement(By.xpath(xpath1));
        elem.clear();
    }

}
